package Reproductor;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */


import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sound.sampled.AudioFileFormat;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javazoom.jl.decoder.BitstreamException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.advanced.*;
import org.tritonus.share.sampled.*;
import javazoom.spi.*;
import org.tritonus.share.sampled.file.TAudioFileFormat;
import java.util.Scanner;
import javazoom.jl.decoder.Equalizer.EQFunction;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.FloatControl;
import javax.sound.sampled.Line.Info;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.Port;
        
public class MP3AD {
    
    private String filename;
    private newAdvancedPlayer player;
    private int position = 0;
    private int sw;
    private float frame_per_milisecond;

    // constructor que tom ael nombre el arhivo MP3
    public MP3AD(String filename) {
        this.filename = filename;  
        sw=0;
    }

    public void close() {         
        if (player != null){
            player.close();
            position = 0;
        } 
    }
    
    public void play() {
        try {
            FileInputStream fis = new FileInputStream(filename);
            BufferedInputStream bis = new BufferedInputStream(fis);
            player = new newAdvancedPlayer(bis);            
        }
        catch (Exception e) {
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }

        new Thread() {
            @Override
            public void run() {
                try {   
                    player.play(position, 2999999);                         
                }
                catch (Exception e) { 
                    System.out.println(e); 
                }
            }
        }.start(); 
        for(int i=0;i<900000;i++){
            double j = Math.sin(i);
        }
        if(sw==0){
            try {
                frame_per_milisecond = player.getFramePerMilisecond();
            } catch (BitstreamException ex) {
                Logger.getLogger(MP3AD.class.getName()).log(Level.SEVERE, null, ex);
            }
        }        
    } 
        
    public void pause()  {
        try {
            position = position + (int) player.getFramePosition();
            player.close(); 
        }
        catch (Exception e){
            System.out.println("Problem playing file " + filename);
            System.out.println(e);
        }
    }
    
    public int getBitrate() {
        return player.getBitrate();
    }
    
    public void setTiempoBarra(int tiempoBarra) {         
        position = /*position*/ + (int)(tiempoBarra*frame_per_milisecond);            
        player.close();
        //this.play();       
    }
    
    public void setTiempoBarra2(int tiempoBarra) {
        position = /*position - */(int)(tiempoBarra*frame_per_milisecond);            
        player.close();
    }
    
    
    public static void main(String[] args) throws InterruptedException{
        String filename = "C:/5.Anakelly - Under my Thumb.mp3";
        MP3AD song = new MP3AD(filename);
        Scanner entrada = new Scanner(System.in);
        Info source = Port.Info.SPEAKER;
        
        song.play();
        //if (AudioSystem.isLineSupported(source)) {
            try  {
                Port outline = (Port) AudioSystem.getLine(source);
                outline.open();                
                FloatControl volumeControl = (FloatControl) outline.getControl(FloatControl.Type.VOLUME);                
                System.out.println("       volume: " + volumeControl.getValue() );
                while(true) {
                    float v = entrada.nextFloat();
                    volumeControl.setValue(v);
                    System.out.println("   new volume: " + volumeControl.getValue() ); 
                }
            } 
            catch (LineUnavailableException ex) {
                System.err.println("source not supported");
                ex.printStackTrace();
            }            
        //}
    }
    
}
